/**
 * Restituisce i metadati di un result set
 * definibile al costruttore o ridefinibile 
 * tramite un metodo setter. Wrapper class
 * dell'interfaccia ResultSetMetaData
 *
 *
 **/
package mwt.xml.xdbforms.dblayer.metadata.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import mwt.xml.xdbforms.dblayer.metadata.TableMetaData;

/**
 *
 * @author Gianfranco Murador
 * Wrapper class dell'interfaccia ResultSetMetaData che il driver manager
 * @see ResultSetMetaData
 */
public class JDBCTableMetaDataDM extends TableMetaData {

    private ResultSet resultSet = null;
    private ResultSetMetaData metaData = null;
    Connection c = null;
    Statement s = null;


    public JDBCTableMetaDataDM() {
    }

    public JDBCTableMetaDataDM(String table, String username, String password, String url) {

        String sql = "SELECT * FROM " + table+ " LIMIT 0,0";
        try {
            c = DriverManager.getConnection(url, username, password);
            s = c.createStatement();
            resultSet =  s.executeQuery(sql);
            metaData = resultSet.getMetaData();
        } catch (SQLException ex) {
            Logger.getLogger(JDBCTableMetaDataDM.class.getName()).log(Level.SEVERE, null, ex);
        }finally{

            /*

            if(c != null){
                try {
                    c.close();
                } catch (SQLException ex) {
                    Logger.getLogger(JDBCTableMetaDataDM.class.getName()).log(Level.SEVERE, null, ex);
                }
            }*/


            if( s!= null){
                try {
                    s.close();
                } catch (SQLException ex) {
                    Logger.getLogger(JDBCTableMetaDataDM.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        }

    }

    public int getColumnCount() throws SQLException {

        return metaData.getColumnCount();
    }

    public boolean isAutoIncrement(int column) throws SQLException {

        return metaData.isAutoIncrement(column);
    }

    public boolean isCaseSensitive(int column) throws SQLException {

        return metaData.isCaseSensitive(column);
    }

    public boolean isSearchable(int column) throws SQLException {
        return metaData.isSearchable(column);
    }

    public boolean isCurrency(int column) throws SQLException {
        return metaData.isCurrency(column);
    }

    public int isNullable(int column) throws SQLException {
        return metaData.isNullable(column);
    }

    public boolean isSigned(int column) throws SQLException {
        return metaData.isSigned(column);
    }

    public int getColumnDisplaySize(int column) throws SQLException {
        return metaData.getColumnDisplaySize(column);
    }

    public String getColumnLabel(int column) throws SQLException {
        return metaData.getColumnLabel(column);
    }

    public String getColumnName(int column) throws SQLException {
        return metaData.getColumnName(column);
    }

    public String getSchemaName(int column) throws SQLException {
        return metaData.getSchemaName(column);
    }

    public int getPrecision(int column) throws SQLException {
        return metaData.getPrecision(column);
    }

    public int getScale(int column) throws SQLException {
        return metaData.getScale(column);
    }

    public String getTableName(int column) throws SQLException {
        return metaData.getTableName(column);
    }

    public String getCatalogName(int column) throws SQLException {
        return metaData.getCatalogName(column);
    }

    public int getColumnType(int column) throws SQLException {
        return metaData.getColumnType(column);
    }

    public String getColumnTypeName(int column) throws SQLException {
        return metaData.getColumnTypeName(column);
    }

    public boolean isReadOnly(int column) throws SQLException {
        return metaData.isReadOnly(column);
    }

    public boolean isWritable(int column) throws SQLException {
        return metaData.isWritable(column);
    }

    public boolean isDefinitelyWritable(int column) throws SQLException {
        return metaData.isDefinitelyWritable(column);

    }

    public String getColumnClassName(int column) throws SQLException {

        return metaData.getColumnClassName(column);

    }

    public <T> T unwrap(Class<T> iface) throws SQLException {
        return metaData.unwrap(iface);
    }

    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return metaData.isWrapperFor(iface);
    }

    @Override
    public void close() {
        if(c!=null){
            try {
                c.close();
            } catch (SQLException ex) {
                Logger.getLogger(JDBCTableMetaDataDM.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
